<template>
    <div style="height: 100%; width: 100%; display: flex; align-items: center; justify-content: center;">
        <div class="personal">
            <div class="personal-left">
                <div class="personal-left-head">
                    <div class="demo-image__preview" style="height: 60%; width: 60%; margin: 10px 0;">
                        <el-avatar shape="circle" style="width: 100%; height: 100%;">
                            <!-- border-radius: 50%; -->
                            <el-image 
                                style="width: 100%; height: 100%;"
                                :src="mallAdmin.avatar" 
                                :preview-src-list="srcList">
                            </el-image>
                        </el-avatar>
                    </div>
                    <h1>超级管理员</h1>
                    <div style="margin: 10px 0;">
                        <span>注册时间：</span>
                        <span>{{ mallAdmin.createTime }}</span>
                    </div>
                    
                    <el-button type="primary" @click="dialogFormVisible = true">修改头像</el-button>
                </div>
                <div class="personal-left-middle">
                    <div style="width: 100%; display: flex; flex-direction: row; align-items: center; justify-content: space-between;">
                        <hr style="width: 40%;height: 1px;"><span style="font-size: 15px; font-weight: bold;">绑定信息</span><hr style="width: 40%;height: 1px;">
                    </div>
                    <div style="display: flex; justify-content: space-between; margin-top: 15px; padding: 0 10px;">
                        <div style="display: flex; align-items: center;">
                            <span>手机：</span><span>{{mallAdmin.phone}}</span>
                        </div>
                        <el-button type="success" @click="dialogFormVisible = true"><i class="el-icon-mobile" style="margin-right: 5px;"></i>修改手机</el-button>
                    </div>
                    <div style="display: flex; justify-content: space-between; margin-top: 15px; padding: 0 10px;">
                        <div style="display: flex; align-items: center;">
                            <span>邮箱：</span><span>{{mallAdmin.email}}</span>
                        </div>
                        <el-button type="primary" @click="dialogFormVisible = true"><i class="el-icon-message" style="margin-right: 5px;"></i>绑定邮箱</el-button>
                    </div>
                </div>
                <div class="personal-left-last">
                    <el-button type="danger" @click="updatePassword"><i class="el-icon-message" style="margin-right: 5px;"></i>修改密码</el-button>
                </div>
            </div>
            <div class="personal-right">
                <div style="height: 100%; width: 100%;">
                    <div style="width: 100%; display: flex; flex-direction: row; align-items: center; justify-content: space-between;">
                        <hr style="width: 40%;height: 1px;"><span style="font-size: 20px; font-weight: bold;">个人信息</span><hr style="width: 40%;height: 1px;">
                    </div>
                    <div class="table" style="padding: 20px 40px; ">
                        <el-descriptions class="margin-top" title="个人信息" :column="1" size="medium" border>
                            <template slot="extra">
                                <el-button type="primary" size="small" @click="dialogFormVisible = true">修改</el-button>
                            </template>
                            <el-descriptions-item :label-class-name="'my-label'" :content-class-name="'my-content'" :label-width="'120px'">
                                <template slot="label">
                                    <i class="el-icon-user"></i>
                                    管理员ID
                                </template>
                                {{mallAdmin.adminId}}
                            </el-descriptions-item>
                            <el-descriptions-item>
                                <template slot="label">
                                    <i class="el-icon-user-solid"></i>
                                    昵称
                                </template>
                                {{mallAdmin.nickname}}
                            </el-descriptions-item>
                            <el-descriptions-item>
                                <template slot="label">
                                    <i class="el-icon-s-check"></i>
                                    性别
                                </template>
                                {{mallAdmin.gender}}
                            </el-descriptions-item>
                            <el-descriptions-item>
                                <template slot="label">
                                    <i class="el-icon-mobile-phone"></i>
                                    手机号
                                </template>
                                {{mallAdmin.phone}}
                            </el-descriptions-item>
                            <el-descriptions-item>
                                <template slot="label">
                                    <i class="el-icon-present"></i>
                                    生日
                                </template>
                                {{mallAdmin.birthday}}
                            </el-descriptions-item>
                            <el-descriptions-item>
                                <template slot="label">
                                    <i class="el-icon-time"></i>
                                    更新时间
                                </template>
                                {{mallAdmin.updateTime}}
                            </el-descriptions-item>
                            <el-descriptions-item>
                                <template slot="label">
                                    <i class="el-icon-time"></i>
                                    上次登录时间
                                </template>
                                {{mallAdmin.lastLoginTime}}
                            </el-descriptions-item>
                        </el-descriptions>
                    </div>
                </div>
            </div>
        </div>
        <el-dialog class="updatedialog" title="修改个人信息" :visible.sync="dialogFormVisible" :center="true" width="35%">
            <el-form :model="form" ref="form" label-width="20%" class="demo-ruleForm">
                <el-form-item label="管理员ID" prop="adminId">
                    <el-input v-model="form.adminId" readonly ></el-input>
                </el-form-item>
                <el-form-item label="昵称" prop="nickname">
                    <el-input v-model="form.nickname"></el-input>
                </el-form-item>
                <el-form-item label="性别" prop="gender">
                    <el-input v-model="form.gender"></el-input>
                </el-form-item>
                <el-form-item label="生日" prop="birthday">
                    <el-date-picker
                        v-model="form.birthday"
                        type="date"
                        placeholder="选择日期"
                        format="yyyy-MM-dd"
                        value-format = "yyyy-MM-dd"
                        style="width: 100%;">
                    </el-date-picker>
                </el-form-item>
                <el-form-item label="电话" prop="phone">
                    <el-input v-model="form.phone"></el-input>
                </el-form-item>
                <el-form-item label="邮箱" prop="email">
                    <el-input v-model="form.email"></el-input>
                </el-form-item>
                <el-form-item label="头像" prop="avatar">
                    <el-upload
                    ref="upload"
                    action="#"
                    :limit="1"
                    list-type="picture-card"
                    :on-change="handleFileChange"
                    :auto-upload="false">
                        <i slot="default" class="el-icon-plus"></i>
                        <div slot="file" slot-scope="{file}">
                            <img
                                class="el-upload-list__item-thumbnail"
                                :src="file.url" alt=""
                            >
                            <span class="el-upload-list__item-actions">
                                <span
                                class="el-upload-list__item-preview"
                                @click="handlePictureCardPreview(file)"
                                >
                                    <i class="el-icon-zoom-in"></i>
                                </span>
                                <span
                                v-if="!disabled"
                                class="el-upload-list__item-delete"
                                @click="handleRemove(file)"
                                >
                                    <i class="el-icon-delete"></i>
                                </span>
                            </span>
                        </div>
                    </el-upload>
                    <el-dialog :visible.sync="dialogVisible">
                        <img width="100%" :src="dialogImageUrl" alt="">
                    </el-dialog>
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialogBtn">
                <el-button type="primary" @click="updatePersonalMsg()">确 定</el-button>
                <el-button @click="resetForm('form')">重 置</el-button>
            </div>
        </el-dialog>
    </div>
</template>

<script>

// import axios from 'axios'

export default {
    name: 'PersonalCenter',
    data() {
        return {
            srcList: [
            'https://fuss10.elemecdn.com/e/5d/4a731a90594a4af544c0c25941171jpeg.jpeg'
            ],
            mallAdmin: {
                adminId: '1232456',
                nickname: '王小虎',
                gender: '男',
                birthday: '2024-1-1',
                updateTime: '2024-1-2 11:10:10',
                lastLoginTime: '2024-1-2 11:10:10',
                createTime: '2024-1-2 11:10:10',
                phone: '123456789',
                email: '123456@qq.com',
                avatar: 'https://fuss10.elemecdn.com/e/5d/4a731a90594a4af544c0c25941171jpeg.jpeg'
            },
            dialogFormVisible: false,
            formLabelWidth: '120px',
            disabled: false,
            picture: null, // 头像文件
            dialogImageUrl: '', // 修改弹框中头像url
            dialogVisible: false,
            form: {
                // adminId: '1232456',
                // nickname: '王小虎',
                // gender: '男',
                // birthday: '2024-1-1',
                // updateTime: '2024-1-1',
                // lastLoginTime: '2024-1-1',
                // createTime: '2024-1-2 11:10:10',
                // phone: '123456789',
                // email: '123456@qq.com',
                // avatar: 'https://fuss10.elemecdn.com/e/5d/4a731a90594a4af544c0c25941171jpeg.jpeg'
            },
        }
    },
    mounted() {
        this.form = JSON.parse(JSON.stringify(this.mallAdmin))
        this.srcList[0] = this.mallAdmin.avatar
    },
    methods: {
        handleFileChange(file, fileList) {
            // console.log(file);
            // console.log(fileList);
            if (fileList.length > 0) {
                this.picture = fileList[0].raw;
                console.log(this.picture);
            } else {
                this.picture = null;
            }
        },
        handleRemove(file) {
            console.log(file);
            this.$refs.upload.clearFiles(); // 清空上传列表
            this.picture = null; // 清除需要上传的图片文件
            this.dialogVisible = false; // 关闭预览弹窗
        },
        handlePictureCardPreview(file) {
            this.dialogImageUrl = file.url;
            this.dialogVisible = true;
        },
        // 弹框中的提交按钮
        updatePersonalMsg(){
            console.log(this.form);
            console.log(this.picture);
            // axios.post("/updatePersonalMsg",{
            //     book: JSON.stringify(this.form),
            //     picture: this.picture
            // },{
            //     headers:{
            //         'Content-Type': 'multipart/form-data'
            //     }
            // }).then((res) => {
            //     console.log(res);
            //     this.dialogFormVisible = false;
            //     // this.getBookList();
            //     this.$message({
            //         type: 'success',
            //         message: '添加成功!'
            //     });
            // }).catch((error) => {
            //     return this.$message.error("删除图书失败：" + error)
            // })
        },
        // 弹框中的重置按钮
        resetForm(formName) {
            this.$refs[formName].resetFields();
        },
        updatePassword() {
            this.$router.push({
                name: 'changePassword',
            })
        }
    }
}
</script>

<style scoped>
.personal{
    height: 70%;
    width: 75%;
    display: flex;
    flex-direction: row;
    border: 1px solid rgb(240, 240, 245);
    box-shadow: 0 5px 7px rgba(0, 0, 0, 0.5);
}

.personal-left {
    height: 100%;
    width: 30%;
}

.personal-left-head {
    width: 100%; 
    height: 60%; 
    display: flex; 
    flex-direction: column; 
    align-items: center;
}

.personal-left-middle {
    width: 100%; 
    height: 25%;
}

.personal-left-last{
    width: 100%; 
    height: 15%;
    border-top: 1px solid #ccc;
    display: flex;
    justify-content: center;
    align-items: center;
}

.personal-left-last button{
    display: flex;
    width: 90%;
    height: 55%;
    flex-direction: row;
    justify-content: center;
    align-items: center;
}

.personal-left-head span{
    font-weight: 600;
}

.personal-right {
    height: 100%;
    width: 70%;
    padding: 20px 30px;
    border-left: 1px solid #ccc; 
}

.my-label {
    background-color: #E1F3D8;
}

.my-content {
    background-color: #FDE2E2;
}

.dialogBtn {
    text-align: center;
}

.demo-ruleForm .el-form-item{
    display: flex;
    align-items: center;
}

.el-form-item__content {
    width: 400px;
}

.demo-ruleForm {
    font-weight: 1000;
}
</style>